package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.MmrmT;
import cn.e4j.modular.crm.mapper.MmrmTMapper;
import cn.e4j.modular.crm.service.MmrmTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 *  产品类别故障维护
 * </p>
 *
 * @author mengyb
 * @since 2019-04-18
 */
@Service
public class MmrmTServiceImpl extends ServiceImpl<MmrmTMapper, MmrmT> implements MmrmTService {
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwahPageList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwahList(dto, page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwahList(Dto dto) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwahList(dto);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getMmrlPageList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getMmrlList(dto, page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getMmrlList(Dto dto) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getMmrlList(dto);
    }

    @Transactional
    @Override
    public void add(MmrmT mmrmT) {
        ShiroUser shiroUser= ShiroKit.getUser();

        mmrmT.setMMRMENT(shiroUser.getDBC());
        mmrmT.setMMRMUSER(shiroUser.getACCOUNT());
        mmrmT.setMMRMGRUP(shiroUser.getORG_ID());
        mmrmT.setMMRMORIU(shiroUser.getACCOUNT());
        mmrmT.setMMRMORIG(shiroUser.getORG_ID());
        mmrmT.setMMRMTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        mmrmT.setMMRMDATE(LocalDate.now());


        mmrmT.insert();

    }
    @Transactional
    @Override
    public void edit(MmrmT mmrmT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        mmrmT.setMMRMMODU(shiroUser.getACCOUNT());

        UpdateWrapper<MmrmT> mmrmTUpdateWrapper = new UpdateWrapper<>();

        mmrmTUpdateWrapper.eq("MMRMENT",mmrmT.getMMRMENT());
        mmrmTUpdateWrapper.eq("MMRM001", mmrmT.getMMRM001());
        mmrmTUpdateWrapper.eq("MMRM002", mmrmT.getMMRM002());

        mmrmT.update(mmrmTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRow(MmrmT mmrmT) {


        UpdateWrapper<MmrmT> mmrmTUpdateWrapper = new UpdateWrapper<>();

        mmrmTUpdateWrapper.eq("MMRMENT",mmrmT.getMMRMENT());
        mmrmTUpdateWrapper.eq("MMRM001", mmrmT.getMMRM001());
        mmrmTUpdateWrapper.eq("MMRM002", mmrmT.getMMRM002());

        mmrmT.delete(mmrmTUpdateWrapper);


    }
    @Transactional
    @Override
    public void delRows(List<MmrmT> mmrmTList) {
        for (MmrmT mmrmT : mmrmTList
        ) {
            UpdateWrapper<MmrmT> mmrmTUpdateWrapper = new UpdateWrapper<>();

            mmrmTUpdateWrapper.eq("MMRMENT",mmrmT.getMMRMENT());
            mmrmTUpdateWrapper.eq("MMRM001", mmrmT.getMMRM001());
            mmrmTUpdateWrapper.eq("MMRM002", mmrmT.getMMRM002());

            mmrmT.delete(mmrmTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(MmrmT mmrmT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        QueryWrapper<MmrmT> mmrmTQueryWrapper = new QueryWrapper<>();
        mmrmTQueryWrapper.eq("MMRMENT", shiroUser.getDBC());
        mmrmTQueryWrapper.eq("MMRM001", mmrmT.getMMRM001());
        mmrmTQueryWrapper.eq("MMRM002", mmrmT.getMMRM002());
        return this.baseMapper.selectCount(mmrmTQueryWrapper)>0?true:false;
    }
}
